// pages/result/result.js
var name;
var that;
var cnchar = require('../../utils/cnchar.min.js').Strophe
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canvasHidden: true,     //设置画板的显示与隐藏，画板不隐藏会影响页面正常显示
    avatarUrl: '',         //用户头像
    nickName: '',          //用户昵称
    wxappName: '测试你',    //小程序名称
    shareImgPath: '',
    screenWidth: '',       //设备屏幕宽度
    description: 'ddd',    //奖品描述
    FilePath: '',           //头像路径
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    name = options.name;
    that=this;

    var charlenth=name.stroke();

    // wx.showModal({
    //   title: '11',
    //   content: name.length * 10 + charlenth+"",
    // })
    var userInfo, nickName, avatarUrl
    //获取用户信息，头像，昵称之类的数据
    // wx.getUserInfo({
    //   success: function (res) {
    //     console.log(res);
    //     userInfo = res.userInfo
    //     nickName = userInfo.nickName
    //     avatarUrl = userInfo.avatarUrl
    //     that.setData({
    //       avatarUrl: res.userInfo.avatarUrl,
    //       nickName: res.userInfo.nickName,
    //     })
    //     // wx.downloadFile({
    //     //   url: res.userInfo.avatarUrl
    //     // })
    //   }
    // })
    wx.getSystemInfo({
      success: res => {
        that.setData({
          screenWidth: res.screenWidth
        })
        console.log(that.data.screenWidth)
      }
    })
  //  createImg((name.length*10+charlenth) % 100+"%",100 - charlenth % 100 + "%");
 //   Math.floor(Math.random() * (max - min + 1) + min);
    createImg(Math.floor(Math.random() * (101 - 1 + 1) ) + "%", Math.floor(Math.random() * (101 - 1 + 1) + 1))
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '看看你被绿的可能性有多少？',
      path: 'pages/test/test',
      success: function (res) {
        wx.showShareMenu({
          withShareTicket: true
        })
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  saveImageToPhotosAlbum :function(e){
    wx.authorize({
      scope: 'scope.writePhotosAlbum',
      success:function(e){
        wx.saveImageToPhotosAlbum({
          filePath: that.data.shareImgPath,
          //保存成功失败之后，都要隐藏画板，否则影响界面显示。
          success: (res) => {
            console.log(res)
          wx.showModal({
            title: '提示',
            content: '已成功保存到您的相册中',
            showCancel: false
          })
          },
          fail: (err) => {
            wx.showModal({
              title: '提示',
              content: '保存失败',
              showCancel:false
            })
          }
        })
      }
    })
    
  }
})
function createImg(other,me){
  //设置画板显示，才能开始绘图
  that.setData({
    canvasHidden: false
  })
  var unit = that.data.screenWidth / 375
  var path1 = "../images/bg4.png"
  var avatarUrl = that.data.avatarUrl
  var path2 = "../images/oo.png"
  var path3 = "../images/3.jpg"
  // var path4 = "../images/4.png"

  if(me<=10){
 var path5 = "../images/1.png"
  }
  else if(10<me&&me<=20){
    var path5 = "../images/2.png"
  }
  else if (20 < me && me<= 30) {
    var path5 = "../images/3.png"
  }
  else if (30 < me && me <= 40) {
    var path5 = "../images/4.png"
  }
  else if (40 < me && me <= 50) {
    var path5 = "../images/5.png"
  }
  else if (50 < me && me<= 60) {
    var path5 = "../images/6.png"
  }
  else if (60 < me && me <= 70) {
    var path5 = "../images/7.png"
  }
  else if (70 < me && me<= 80) {
    var path5 = "../images/8.png"
  }
  else if (80 < me && me<= 90) {
    var path5 = "../images/9.png"
  }
  else if (90 < me && me <= 100) {
    var path5 = "../images/10.png"
  }
  else{
    var path5 = "../images/11.png"
  }
  // var unlight = "../images/5.png"
  var nickName = that.data.nickName
  console.log(nickName + "昵称")
  var context = wx.createCanvasContext('share')
  var description = that.data.description
  var wxappName = "来「 测试你被绿 」概率有多大"
 // context.drawImage(path1, 0, 0, unit * 375, unit * 500.5)
  //  context.drawImage(path4, unit * 164, unit * 40, unit * 50, unit * 50)
  context.drawImage(path2, unit * 78, unit * 98, unit * 220, unit * 78)
  context.drawImage(path5, unit * 78, unit * 250, unit * 220, unit * 158)
  // context.drawImage(unlight, unit * 82, unit * 145, unit * 22, unit * 32)
  // context.drawImage(unlight, unit * 178, unit * 145, unit * 22, unit * 32)
  // context.drawImage(unlight, unit * 274, unit * 145, unit * 22, unit * 32)
  // context.drawImage(unlight, unit * 82, unit * 240, unit * 22, unit * 32)
  // context.drawImage(unlight, unit * 178, unit * 240, unit * 22, unit * 32)
  // context.drawImage(unlight, unit * 274, unit * 240, unit * 22, unit * 32)

  //名字
  context.setFontSize(25)
  context.setFillStyle("#00CD00")
  context.setTextAlign("center");
  context.fillText(name + "", unit * 375/2,49)

  //绿别人概率
  context.setFontSize(30)
  context.setFillStyle("#00CD00")
  context.setTextAlign("center");
  context.fillText(other, unit * 375 / 2, unit*222)

//线
  context.moveTo(0, unit * 500.5/2);
  context.lineWidth = 1;         
  context.setStrokeStyle("#ffffff");
  context.lineTo(unit * 375, unit * 500.5 / 2)
    context.stroke();      

  // //绿别人概率
  // context.setFontSize(25)
  // context.setFillStyle("#ffffff")
  // context.setTextAlign("center");
  // context.fillText(me, unit * 375 / 2, unit * 420)  


//小程序二维码
  var timestamp = Date.parse(new Date());
  timestamp = timestamp / 1000;
  var n = timestamp * 1000;
  var date = new Date(n);
  //年
  var Y = date.getFullYear();
  //月
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  //日
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();

  if (M==9){
    if (D == 9 || D == 10 || D ==11){
      var path11 = "../images/11.png"
      context.drawImage(path11, unit * 20, unit * 500.5 - unit * 75, unit * 55, unit * 55)
      context.setTextAlign("left");
      context.setFontSize(14)
      context.setFillStyle("#ff9800")
      context.fillText("这是有多大的勇气", unit * 90, unit * 500.5 - unit * 45)
      context.fillText(wxappName, unit * 90, unit * 500.5 - unit * 25)
    }
    else{
      context.drawImage(path3, unit * 20, unit * 500.5 - unit * 75, unit * 55, unit * 55)
      context.setTextAlign("left");
      context.setFontSize(14)
      context.setFillStyle("#ff9800")
      context.fillText("长按识别小程序", unit * 90, unit * 500.5 - unit * 45)
      context.fillText(wxappName, unit * 90, unit * 500.5 - unit * 25)
    }
  }
  else {
    context.drawImage(path3, unit * 20, unit * 500.5 - unit * 75, unit * 55, unit * 55)
    context.setTextAlign("left");
    context.setFontSize(14)
    context.setFillStyle("#ff9800")
    context.fillText("长按识别小程序", unit * 90, unit * 500.5 - unit * 45)
    context.fillText(wxappName, unit * 90, unit * 500.5 - unit * 25)

  }
 
  //   context.drawImage(path4, 48, 200, 280, 128)



  //把画板内容绘制成图片，并回调 画板图片路径
  context.draw(false, function () {
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: unit * 375,
      height: unit * 500.5,
      destWidth: unit * 375,
      destHeight: unit * 500.5,
      canvasId: 'share',
      success: function (res) {
        that.setData({
          shareImgPath: res.tempFilePath
        })
        if (!res.tempFilePath) {
          wx.showModal({
            title: '提示',
            content: '图片绘制中，请稍后重试',
            showCancel: false
          })
        }
        console.log(that.data.shareImgPath)
        //画板路径保存成功后，调用方法吧图片保存到用户相册
        // wx.saveImageToPhotosAlbum({
        //   filePath: res.tempFilePath,
        //   //保存成功失败之后，都要隐藏画板，否则影响界面显示。
        //   success: (res) => {
        //     console.log(res)
        //     wx.hideLoading()
        //     that.setData({
        //       canvasHidden: true
        //     })
        //   },
        //   fail: (err) => {
        //     console.log(err)
        //     wx.hideLoading()
        //     that.setData({
        //       canvasHidden: true
        //     })
        //   }
        // })
      }
    })
  });
}